package com.koron.css2.systemManage.servlet;

import com.koron.css2.ApplicationConfig;
import com.koron.css2.ServerInterface;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.css2.systemManage.bean.OrgUserBean;
import com.koron.css2.systemManage.bean.PubSysUserPostBean;
import com.koron.css2.systemManage.bean.UserRangeBean;
import com.koron.css2.systemManage.mapper.OrgUserMapper;
import com.koron.css2.systemManage.mapper.PostUserMapper;
import com.koron.css2.systemManage.service.IDataRangeService;
import com.koron.css2.systemManage.service.IRoleService;
import com.koron.css2.systemManage.service.ServiceFactory;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class OrgUserUpdate implements ServerInterface {
	
	private static Logger log = LogManager.getLogger(OrgUserUpdate.class);

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<Map> msg = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "保存成功", Map.class);
		try {
			IRoleService service = ServiceFactory.getRoleService();
			IDataRangeService dataRangeService = ServiceFactory.getDataRangeService();
			OrgUserBean user = JsonUtils.objectToPojo(req.getData(), OrgUserBean.class);
			if(StringUtils.isEmpty(user.getAccount())) {
				msg.setCode(Constant.MESSAGE_INT_FAIL);
				msg.setDescription("用户账号不能为空");
				return msg;
			}
			String roleCode = user.getRoleCode();
			PostUserMapper postUserMapper = factory.getMapper(PostUserMapper.class);
			postUserMapper.deleteUserPostByAcccount(user.getAccount());
			if(StringUtils.isNotEmpty(user.getPostNo())) {
				String postNo = user.getPostNo();
				String[] postArray = postNo.split(",");
				for(String post : postArray) {
					PubSysUserPostBean userPost = new PubSysUserPostBean();
					userPost.setCreateInfo(userInfo);
					userPost.setUserAccount(user.getAccount());
					userPost.setPostNo(post);
					postUserMapper.addUserPost(userPost);
				}
			}
			OrgUserMapper orgUserMapper = factory.getMapper(OrgUserMapper.class);
			OrgUserBean newUser = orgUserMapper.selectByAccount(user.getAccount());
			if(StringUtils.isNotEmpty(roleCode)) {
				String[] roleArray = roleCode.split(",");
				service.deleteRoleUser(newUser);
				for(String role : roleArray) {
					service.addRoleUser(role, Arrays.asList(newUser));
				}
			} else {
				service.deleteRoleUser(newUser);
			}
			List<UserRangeBean> ranges = dataRangeService.getUserRangeValueList(user.getAccount(), ApplicationConfig.getAppCustomerAppid(), "company");
			List<String> companyList = ranges.stream().map(t -> t.getValue()).collect(Collectors.toList());
			boolean b = companyList.stream().anyMatch(task -> userInfo.getCurWaterCode().equals(task));
			if(b == false) {
				dataRangeService.addUserRangeValue(ApplicationConfig.getAppCustomerAppid(), user.getAccount(), "company", Arrays.asList(userInfo.getCurWaterCode()));
			}
			OrgUserMapper orgUser = factory.getMapper(OrgUserMapper.class);
			orgUser.update(user);
		} catch (Exception e) {
			log.error("修改用户出错：" + e);
			msg.setCode(Constant.MESSAGE_INT_FAIL);
			msg.setDescription("修改用户出错");
		}
		return msg;
	}
}
